package com.tanhua.app.service;

import cn.hutool.core.collection.CollectionUtil;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.UserLocationApi;
import com.tanhua.db.UserInfo;
import com.tanhua.vo.NearUserVo;
import com.tanhua.vo.UserHolder;
import com.tanhua.vo.UserLocationVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BaiduService {

    @Reference
    private UserLocationApi userLocationApi;


    public ResponseEntity toAddr(double x_latitude, double y_longitude, String addrStr) {

        userLocationApi.toAddr(x_latitude,y_longitude,addrStr, UserHolder.getUser().getId());

        return ResponseEntity.ok(null);
    }

    @Reference
    private UserInfoApi userInfoApi;
    public ResponseEntity search(String gender, Integer distance) {
        //查询附近的人
        Long id = UserHolder.getUser().getId();
        List<UserLocationVo> userLocationVoList = userLocationApi.search(gender, distance, UserHolder.getUser().getId());
        List<NearUserVo> nearUserVoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(userLocationVoList)){
            for (UserLocationVo userLocationVo : userLocationVoList) {
                UserInfo userInfo = userInfoApi.findByUserId(userLocationVo.getUserId());
                //过滤自己
                if (UserHolder.getUser().getId()==userInfo.getId()) {
                    continue;
                }if (!gender.equals(userInfo.getGender())){
                    continue;
                }
                NearUserVo nearUserVo = new NearUserVo();
                nearUserVo.setAvatar(userInfo.getAvatar());
                nearUserVo.setNickname(userInfo.getNickname());
                nearUserVo.setUserId(userInfo.getId());
                nearUserVoList.add(nearUserVo);
            }
        }
        return  ResponseEntity.ok(nearUserVoList);
    }
}
